package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.role.*;
import com.glink.manage.service.RoleMenuRelaService;
import com.glink.manage.service.RoleService;
import com.glink.manage.vo.menu.MenuSimpleInfoVO;
import com.glink.manage.vo.role.RoleBaseVO;
import com.glink.manage.vo.role.RoleSimpleVO;
import com.glink.manage.vo.role.RoleUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/API/Manage/role")
@Api(tags = "角色管理")
public class RoleController extends AbstractBaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuRelaService roleMenuRelaService;

    @ApiOperation(value = "01.1.新增角色", notes = "01.1.新增角色")
    @PostMapping(value = "/insertRole", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertRole(@Validated @RequestBody RoleSaveRequestDTO dto) throws GeneralSimpleException {
        log.info("01.1 新增角色 {}", LogUtil.encode(dto));
        boolean i = roleService.insertRole(dto);
        log.info("01.新增角色-success");
        return this.response(i);
    }

    @ApiOperation(value = "01.2 修改角色", notes = "01.2 修改角色")
    @PostMapping(value = "/modifyRole", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> modifyRole(@Validated @RequestBody RoleUpdateRequestDTO dto) throws GeneralSimpleException {
        log.info("01.2 新增或修改角色 {}", LogUtil.encode(dto));
        boolean i = roleService.modifyRole(dto);
        log.info("01.2 新增或修改角色-success");
        return this.response(i);
    }

    @ApiOperation(value = "02 根据角色id查询", notes = "02 根据角色id查询")
    @PostMapping(value = "/findByRoleId", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<RoleBaseVO> findByRoleId(@Validated @RequestBody RoleIdRequestDTO roleIdRequestDTO) {
        log.info("02 根据角色id查询 {}", LogUtil.encode(roleIdRequestDTO));
        RoleBaseVO roleBaseVO = roleService.findByRoleId(roleIdRequestDTO.getRoleId());
        return this.response(roleBaseVO);
    }

    @ApiOperation(value = "04.角色信息分页", notes = "04.角色信息分页")
    @PostMapping(value = "/findRolePage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<RoleUserVO>> getRolePage(@RequestBody @Validated RolePageRequestDTO pageRequestVO) {
        log.info("04.角色信息分页 {}", LogUtil.encode(pageRequestVO));
        IPage<RoleUserVO> iPage = roleService.getRolePage(pageRequestVO);
        log.info("04.角色信息分页：success");
        return this.response(iPage);
    }

    @ApiOperation(value = "06.修改角色菜单关系（单个）")
    @PostMapping(value = "/modifyRoleMenuRelaOne", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> modifyRoleMenuRelaOne(@Validated @RequestBody RoleMenuIdRequestDTO dto) {
        log.info("06.修改角色菜单关系（单个） {}", LogUtil.encode(dto));
        boolean status = roleMenuRelaService.modifyRoleMenuRelaOne(dto);
        log.info("06.修改角色菜单关系（单个） success");
        return this.response(status);
    }
    
    @ApiOperation(value = "07.根据角色id删除")
    @PostMapping(value = "/deleteRoleById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Object> deleteRoleById(@Validated @RequestBody RoleIdRequestDTO roleIdRequestDTO) throws GeneralSimpleException {
        log.info("07.根据角色id删除 {}", LogUtil.encode(roleIdRequestDTO));
        int i = roleService.deleteRoleById(roleIdRequestDTO.getRoleId());
        return this.response(i);
    }

    @ApiOperation(value = "08.修改角色菜单关系（批量）")
    @PostMapping(value = "/modifyRoleMenuRela", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> modifyRoleMenuRela(@Validated @RequestBody RoleMenuIdListRequestDTO dto) {
        log.info("08.修改角色菜单关系 {}", LogUtil.encode(dto));
        boolean status = roleMenuRelaService.modifyRoleMenuRela(dto);
        log.info("08.修改角色菜单关系 success");
        return this.response(status);
    }

    @ApiOperation(value = "09.根据角色id获取菜单权限")
    @PostMapping(value = "/findMenuByRoleId", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<MenuSimpleInfoVO>> findMenuByRoleId(@Validated @RequestBody RoleIdRequestDTO requestDTO) throws GeneralSimpleException {
        log.info("09.根据角色id获取菜单权限 {}", LogUtil.encode(requestDTO));
        List<MenuSimpleInfoVO> simpleInfoVOList = roleMenuRelaService.findByRoleId(requestDTO.getRoleId());
        log.info("09.根据角色id获取菜单权限：success");
        return this.response(simpleInfoVOList);
    }

    @ApiOperation(value = "10.获取角色列表")
    @GetMapping(value = "/getRoleList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<RoleSimpleVO>> getRoleList() throws GeneralSimpleException {
        log.info("10.获取角色列表");
        List<RoleSimpleVO> simpleInfoVOList = roleService.getRoleList();
        log.info("10.获取角色列表：success");
        return this.response(simpleInfoVOList);
    }
}
